package ch.dreamweb.readpractice.client.lesetraining.test.view;

import ch.dreamweb.readpractice.client.lesetraining.Constants.Test;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class MetaTestViewImpl extends Composite implements MetaTestView {

	private static MetaViewUiBinder uiBinder = GWT
			.create(MetaViewUiBinder.class);

	@UiTemplate("MetaTestView.ui.xml")
	interface MetaViewUiBinder extends UiBinder<Widget, MetaTestViewImpl> {
	}

	protected interface CssStyle extends CssResource {
	}

	@UiField
	CssStyle style;

	@UiField
	Button buttonTest1;
	@UiField
	Button buttonTest2;
	@UiField
	Button buttonTest3;
	@UiField
	Button buttonTest4;
	@UiField
	Button buttonTest5;
	@UiField
	Button buttonTest6;
	@UiField
	Button buttonSoundTest;

	@UiField
	Button buttonLogout;
	@UiField
	Label labelInfoLogout;
	@UiField
	Label labelHead;

	private Presenter presenter;

	public MetaTestViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		// Can access @UiField after calling createAndBindUi

		buttonSoundTest.setText("Soundtest");
		buttonSoundTest.setTabIndex(0);
		buttonTest1.setText("1) Zahlen nachsprechen");
		buttonTest1.setTabIndex(1);
		buttonTest2.setText("2) Wortlistenvergleich");
		buttonTest2.setTabIndex(2);
		buttonTest3.setText("3) Labyrinth 2d");
		buttonTest3.setTabIndex(3);
		buttonTest4.setText("4) Labyrinth 3d");
		buttonTest4.setTabIndex(4);
		buttonTest5.setText("5) Multitasking");
		buttonTest5.setTabIndex(1);
		buttonTest6.setText("6) Zahlen rückwärts");
		buttonTest6.setTabIndex(2);
		buttonSoundTest.setVisible(true);
		buttonTest1.setVisible(true);
		buttonTest2.setVisible(true);
		buttonTest3.setVisible(true);
		buttonTest4.setVisible(true);
		buttonTest5.setVisible(true);
		buttonTest6.setVisible(true);

		labelInfoLogout
				.setText("Das Training ist abgeschlossen, du kannst nun ausloggen.");
		buttonLogout.setText("Logout");
		setLogoutVisible(false);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@UiHandler("buttonTest1")
	void onButtonTest1Click(ClickEvent e) {
		if (presenter != null) {
			presenter.onTestButtonClicked(Test.TEST1);
		}
	}

	@UiHandler("buttonTest2")
	void onButtonTest2Click(ClickEvent e) {
		if (presenter != null) {
			presenter.onTestButtonClicked(Test.TEST2);
		}
	}

	@UiHandler("buttonTest3")
	void onButtonTest3Click(ClickEvent e) {
		if (presenter != null) {
			presenter.onTestButtonClicked(Test.TEST3);
		}
	}

	@UiHandler("buttonTest4")
	void onButtonTest4Click(ClickEvent e) {
		if (presenter != null) {
			presenter.onTestButtonClicked(Test.TEST4);
		}
	}

	@UiHandler("buttonTest5")
	void onButtonTest5Click(ClickEvent e) {
		if (presenter != null) {
			presenter.onTestButtonClicked(Test.TEST5);
		}
	}

	@UiHandler("buttonTest6")
	void onButtonTest6Click(ClickEvent e) {
		if (presenter != null) {
			presenter.onTestButtonClicked(Test.TEST6);
		}
	}

	@UiHandler("buttonSoundTest")
	void onButtonSoundTestClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onTestButtonClicked(Test.SOUNDTEST);
		}
	}

	@UiHandler("buttonLogout")
	void onButtonLogoutClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onLogoutButtonClicked();
		}

	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setTestEnabled(Test test, boolean enabled) {
		switch (test) {
		case ALL:
			buttonTest1.setEnabled(enabled);
			buttonTest2.setEnabled(enabled);
			buttonTest3.setEnabled(enabled);
			buttonTest4.setEnabled(enabled);
			buttonTest5.setEnabled(enabled);
			buttonTest6.setEnabled(enabled);
			break;
		case TEST1:
			buttonTest1.setEnabled(enabled);
			break;
		case TEST2:
			buttonTest2.setEnabled(enabled);
			break;
		case TEST3:
			buttonTest3.setEnabled(enabled);
			break;
		case TEST4:
			buttonTest4.setEnabled(enabled);
			break;
		case TEST5:
			buttonTest5.setEnabled(enabled);
			break;
		case TEST6:
			buttonTest6.setEnabled(enabled);
			break;
		default:
			break;
		}
	}

	@Override
	public void setTestVisible(Test test, boolean enabled) {
		switch (test) {
		case ALL:
			buttonTest1.setVisible(enabled);
			buttonTest2.setVisible(enabled);
			buttonTest3.setVisible(enabled);
			buttonTest4.setVisible(enabled);
			buttonTest5.setVisible(enabled);
			buttonTest6.setVisible(enabled);
			break;
		case TEST1:
			buttonTest1.setVisible(enabled);
			break;
		case TEST2:
			buttonTest2.setVisible(enabled);
			break;
		case TEST3:
			buttonTest3.setVisible(enabled);
			break;
		case TEST4:
			buttonTest4.setVisible(enabled);
			break;
		case TEST5:
			buttonTest5.setVisible(enabled);
			break;
		case TEST6:
			buttonTest6.setVisible(enabled);
			break;
		default:
			break;
		}
	}

	@Override
	public void showFootprints(int recordLevel, int recordDayLevel, int panel) {
	}

	@Override
	public void setLogoutVisible(boolean visible) {
		labelInfoLogout.setVisible(visible);
		buttonLogout.setVisible(visible);
		buttonLogout.setFocus(visible);
	}

	@Override
	public void setLabelHead(int testday, String username) {
		labelHead.setText("Benutzer: " + username + " / Übungstag: " + testday);
	}
}
